package site.addzero.repl

import kotlinx.coroutines.runBlocking
import org.koin.core.annotation.Single
import site.addzero.app.AdvancedRepl
import site.addzero.app.ParamDef
import site.addzero.cli.platform.runCmd
import kotlin.reflect.typeOf

/**
 * GitConfigRepl的REPL包装类，实现AdvancedRepl接口
 * 专门用于设置Git用户名和邮箱
 */
@Single
class GitConfigRepl : AdvancedRepl<GitConfigRepl.GitConfigParams, String> {
    override val command: String = "git-config"
    override val description: String = "设置Git用户名和邮箱"
    override val paramDefs: List<ParamDef> = listOf(
        ParamDef(
            name = "username",
            type = typeOf<String>(),
            description = "Git用户名"
        ),
        ParamDef(
            name = "email",
            type = typeOf<String>(),
            description = "Git邮箱"
        )
    )

    data class GitConfigParams(
        val username: String,
        val email: String
    )

    override fun eval(params: GitConfigParams): String = runBlocking {
        val username = params.username
        val email = params.email

        // 设置Git用户名
        val usernameResult = "git config --global user.name \"$username\"".runCmd()
        // 设置Git邮箱
        val emailResult = "git config --global user.email \"$email\"".runCmd()

        if (usernameResult.isSuccess() && emailResult.isSuccess()) {
            println("Git 用户名已设置: $username")
            println("Git 邮箱已设置: $email")
            "Git配置设置成功"
        } else {
            println("Git配置设置失败:")
            if (usernameResult.isError()) {
                println("设置用户名失败: ${usernameResult.error}")
            }
            if (emailResult.isError()) {
                println("设置邮箱失败: ${emailResult.error}")
            }
            "Git配置设置失败"
        }
    }

//    override fun createParams(values: List<Any?>): GitConfigParams {
//        return GitConfigParams(
//            username = values[0] as String,
//            email = values[1] as String
//        )
//    }
}
